package com.test5.liang.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.test5.liang.dao.UserMapper;
import com.test5.liang.pojo.User;
import com.test5.liang.service.UserService;
import com.test5.liang.service.registerLogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import com.test5.liang.pojo.registerLog;

import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/user")
public class userController {

    @Autowired
    registerLogService registerLogService;

    @Autowired
    UserService userService;
    

    /**
     * @Author huangzhenliang
     * @Description //TODO 黄臻良
     * @Date 22:04 2021/12/12
     * @Param 对用户上传的今日打卡信息进行处理
     * @return
     **/
    @RequestMapping("/uploadDaily")
    public String uploadDaily(registerLog registerLog, @RequestParam("username")String username,Model model){

        System.out.println(registerLog.toString());

        User user = userService.findUserByName(username);
        System.out.println(user.toString());
        registerLog.setUserId(user.getUserId());
        registerLog.setCreateTime(new Date());
        registerLog.setStatus(1);
        //判断健康状态
          //若健康为1,否则为0
        if (registerLog.getUserTemperature()<=37&&registerLog.getUserTemperature()>36){
            registerLog.setHealthyStatus(1);
        }else{
            registerLog.setHealthyStatus(0);
        }
        int i = registerLogService.insertRegisterLog(registerLog);
        model.addAttribute("username",username);

        return "dailyRegistersuccess";
    }

    /**
     * @Author huangzhenliang
     * @Description //TODO 黄臻良
     * @Date 9:18 2021/12/13
     * @Param  查看用户信息
     * @return
     **/
    @RequestMapping("/userInfo")
    public String userInfo(@RequestParam("username")String username, Model model){
        User user = userService.findUserByName(username);
        model.addAttribute("user",user);
        System.out.println("****************");
        System.out.println(user.toString());
        String role;
        if (user.getUserRole()==1){
            role = "用户";
        }else {
            role = "管理员";
        }
        model.addAttribute("username",username);
        model.addAttribute("role",role);
        return "userInfo";
    }

     /**
     * @Author huangzhenliang
     * @Description //TODO 黄臻良
     * @Date 23:53 2021/12/13
     * @Param  跳转到每日签到界面，跳转前先判断一下今天有没有登录
     * @return
     **/
     @RequestMapping("/dailyRegister")
     public String dailyRegister(@RequestParam("username")String username, Model model){
         User user = userService.findUserByName(username);
         System.out.println(user.toString());
         model.addAttribute("user",user);

         //查看今日是否已经登录
         registerLog registerLog = registerLogService.selectTodayRegisterLogbyUserId(user.getUserId());
         //registerLog不为null说明今日已签到
         if (registerLog != null){
             model.addAttribute("username",username);
             model.addAttribute("registerLog",registerLog);
             return "userIndexedit";
         }else {
             model.addAttribute("username",username);
             return "userIndex";
         }
     }

    /**
     * @Author huangzhenliang
     * @Description //TODO 黄臻良
     * @Date 23:53 2021/12/13
     * @Param  签到成功展示页面
     * @return
     **/
    @RequestMapping("/dailyRegistersuccess")
    public String dailyRegistersuccess(@RequestParam("username")String username, Model model){
        model.addAttribute("username",username);
        return "dailyRegistersuccess";
    }

     /**
     * @Author huangzhenliang
     * @Description //TODO 黄臻良
     * @Date 11:54 2021/12/14
     * @Param  修改打卡信息
     * @return
     **/
     @RequestMapping("/dailyRegisteredit")
     public String dailyRegisteredit(@RequestParam("username")String username,registerLog registerLog, Model model){
         System.out.println("update---------"+registerLog.toString());
         model.addAttribute("username",username);
         registerLogService.updateRegisterLog(registerLog);
         return "dailyRegistersuccess";
     }

     /**
      * @Author huangzhenliang
      * @Description //TODO 黄臻良
      * @Date 13:30 2021/12/14
      * @Param 用户打卡浏览记录
      * @return
      **/
     @RequestMapping("/RegisterHistory")
     public String RegistereditHistory(@RequestParam(required = false,defaultValue = "1",value = "pagenum")int pagenum,@RequestParam("username")String username,registerLog registerLog, Model model){
         User user = userService.findUserByName(username);

         PageHelper.startPage(pagenum, 6);  //pagenum：页数，pagesize:每页的信息数

         List<registerLog> registerLogs = registerLogService.selectAllRegisterLogByUserId(user.getUserId());//调用业务层查询方法

         PageInfo<registerLog> pageInfo = new PageInfo<>(registerLogs); //得到分页结果对象

         model.addAttribute("pageInfo", pageInfo);  //携带分页结果信息

         model.addAttribute("registerLogs",registerLogs);
         model.addAttribute("username",username);
         return "Registerhistory";
     }

      /**
      * @Author huangzhenliang
      * @Description //TODO 黄臻良
      * @Date 19:06 2021/12/14
      * @Param 修改个人信息
      * @return
      **/
      @RequestMapping("/updateuser")
      public String updateuser(User user, Model model){
          System.out.println("update----" + user.toString());
          User user1 = userService.findUserByName(user.getUserName());
          user.setUserId(user1.getUserId());
          userService.updateUser(user);
          return userInfo(user.getUserName(),model);
      }





}